/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.prueba.models;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 *
 * @author Administrador
 */
public class Floyd {

    //static int[][] floyd;
    //static BufferedReader leer = new BufferedReader(new InputStreamReader(System.in));


    public static void main(String[] args) {

        int n = 0;


            System.out.print("Ingrese n (tamaño de la matriz n X n) : ");

            n = 7;

            int [][] floyd = {       {-1,5,3,-1,-1,-1,-1},
                                    {5,-1,1,5,2,-1,-1},
                                    {3,1,-1,7,-1,-1,12},
                                    {-1,5,7,-1,3,-1,3},
                                    {-1,2,-1,3,-1,1,-1},
                                    {-1,-1,-1,1,1,-1,-1},
                                    {-1,-1,12,3,-1,4,-1}
                                    };

//            for (int i = 0; i < n; i++) {
//                for (int j = 0; j < n; j++) {
//
//                    System.out.println("Inserte la componente W(" + i + ")(" + j +
//                    ")");
//
//                    floyd[i][j] = Integer.parseInt(leer.readLine());
//
//                }
//            }

      
        for (int k = 0; k <= n - 1; k++) {

            for (int i = 0; i <= n - 1; i++) {
                for (int j = 0; j <= n - 1; j++) {
                    if ((floyd[i][k] != -1) && (floyd[k][j] != -1)) {
                        floyd[i][j] = funcionfloyd(floyd[i][j], floyd[i][k] + floyd[k][j]);
                    }
                }
            }

        }

        System.out.println("Matriz de adyacencia correspondiente: ");

        for (int i = 0; i < n; i++) {

            for (int j = 0; j < n; j++) {
                System.out.print(" – "+floyd[i][j]);
            }
            

        System.out.println();

        }

    }

    public static int funcionfloyd(int A, int B) {

        if ((A == -1) && (B == -1)) {
            return -1;
        } else if (A == -1) {
            return B;
        } else if (B == -1) {
            return A;
        } else if (A > B) {
            return B;
        } else {
            return A;
        }

    }
}